<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        #app {
            width: 500px;
            margin: 100px auto;
            background-color: antiquewhite;
        }

        h1 {
            text-align: center;
        }

        table {
            border: 1px solid #ccc;
            width: 100%;
        }

        th,
        td {
            height: 30px;
            line-height: 30px;
            text-align: center;
            border: 1px solid #ccc;
        }
        .grid .total {
            height: 30px;
            line-height: 30px;
            background-color: antiquewhite;
            border-top: 1px solid #ccc;
        }
    </style>
</head>

<body>
    <div id="app">
        <div class="grid">
            <div>
                <h1>图书管理</h1>
                <div class="book">
                    <div>
                        <label for="">
                            编号：<input type="text" id="id" v-model="id" disabled="false">
                        </label>
                        <label for="">
                            名称：<input type="text" id="name" v-model="name">
                        </label>
                        <button @click.prevent="handle" :disabled="submitFlag">提交</button>
                    </div>
                </div>
            </div>
            <div class="total">
                <span>图书总数:</span>
                <span>{{total}}</span>
            </div>
            <table cellspacing="0">
                <thead>
                    <tr>
                        <th>编号</th>
                        <th>名称</th>
                        <th>时间</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    <tr :key="item.id" v-for="item in books">
                        <td>{{item.id}}</td>
                        <td>{{item.name}}</td>
                        <td>{{item.data}}</td>
                        <td>
                            <a href="" @click.prevent="edit(item.id)">修改</a>
                            <span>|</span>
                            <a href="" @click.prevent="dele(item.id)">删除</a>
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>

    <script src="./public/js/vue.min.js"></script>
    <script src="./public/js/axios.min.js"></script>
    <script>
        axios.defaults.baseURL = 'http://localhost:3000'

        // axios.interceptors.response.use((res) => {
        //     return res.data;
        // },function(err) {
        //     console.log(err);
        // })

        //焦点的获取
        Vue.directive('focus',{
            inserted: function(el) {
                el.focus();
            }
        })
        
        var vm = new Vue({
            el: '#app',
            data: {
                submitFlag: false,
                flag: false,
                id: '',
                name: '',
                books: [],
            },
            methods: {
                handle: async function () {
                    if (this.flag) { 
                        const ret = await axios.put('books/' + this.id,{
                            name: this.name
                        })
                        if(ret.status == 200) {
                            this.queryData();
                        }
                        this.flag = false;
                    } else {
                        const ret = await axios.post('books',{
                            name: this.name
                        });
                        if(ret.status == 200) {
                            this.queryData();
                        }
                    }
                },
                async edit(id) {
                    //根据id 查询数据 通过 filter 方法 筛选
                    const ret = await axios.get('books/' + id);
                    // console.log(ret.data);
                    this.id = ret.data[0].id;
                    this.name = ret.data[0].name;
                    this.flag = true;
                },
                async dele(id) {
                    const ret = await axios.delete('books/' + id);
                    if(ret.status == 200) {
                        this.queryData();
                    }
                },
                async queryData() {
                    const ret = await axios.get('books');
                    this.books = ret.data
                    // console.log(ret.data);
                }
            },
            computed: {
                total() {
                    //计算图书总数
                    return this.books.length
                }
            },
            watch: {
                async name() {
                    //验证 图书名称是否已经存在
                    var flag = await axios.get('books/book/' + this.name);
                    if(flag.status == 1) {
                        this.submitFlag = true;
                    }else {
                        this.submitFlag = false;
                    }
                }
            },
            mounted: function() {
                //用于获取后台数据
                
                this.queryData();
            }
        });
    </script>
</body>

</html>